Skip to content

Method: offset(int, int)

1: /*
2: * Copyright © 2021 Fachhochschule für die Wirtschaft (FHDW) Hannover
3: *
4: * This file is part of ipspiel21-tictactoe-core.
5: *
6: * ipspiel21-tictactoe-core is free software: you can redistribute it and/or modify it under
7: * the terms of the GNU General Public License as published by the Free Software
8: * Foundation, either version 3 of the License, or (at your option) any later
9: * version.
10: *
11: * ipspiel21-tictactoe-core is distributed in the hope that it will be useful, but WITHOUT
12: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14: * details.
15: *
16: * You should have received a copy of the GNU General Public License along with
17: * ipspiel21-tictactoe-core. If not, see <http://www.gnu.org/licenses/>.
18: */
19: package de.fhdw.gaming.ipspiel21.viergewinnt.core.domain;
20:
21: /**
22: * Represents a field position on a VierGewinnt board. It contains a row number
23: * and a column number (both zero-based).
24: */
25: public final class VierGewinntPosition {
26:
27: /**
28: * The row index.
29: */
30: private final int row;
31: /**
32: * The column index.
33: */
34: private final int column;
35:
36: /**
37: * Creates a position on a VierGewinnt board.
38: *
39: * @param row The row number (zero based).
40: * @param column The column number (zero-based).
41: */
42: private VierGewinntPosition(final int row, final int column) {
43: this.row = row;
44: this.column = column;
45: }
46:
47: /**
48: * Creates a position on a VierGewinnt board.
49: *
50: * @param row The row number (zero based).
51: * @param column The column number (zero-based).
52: * @return The position.
53: */
54: public static VierGewinntPosition of(final int row, final int column) {
55: return new VierGewinntPosition(row, column);
56: }
57:
58: /**
59: * Returns the zero-based row number.
60: */
61: public int getRow() {
62: return this.row;
63: }
64:
65: /**
66: * Returns the zero-based column number.
67: */
68: public int getColumn() {
69: return this.column;
70: }
71:
72: /**
73: * Creates a position relative to this one. The position is not checked against
74: * any bounds.
75: *
76: * @param rowOffset The row offset. May be negative, positive, or zero.
77: * @param columnOffset The column offset. May be negative, positive, or zero.
78: * @return The resulting position.
79: */
80: public VierGewinntPosition offset(final int rowOffset, final int columnOffset) {
81: return VierGewinntPosition.of(this.row + rowOffset, this.column + columnOffset);
82: }
83:
84: @Override
85: public String toString() {
86: return String.format("%c%d", (char) (this.column + 'A'), this.row + 1);
87: }
88:
89: @Override
90: public int hashCode() {
91: return this.row ^ this.column;
92: }
93:
94: @Override
95: public boolean equals(final Object obj) {
96: if (obj instanceof VierGewinntPosition) {
97: final VierGewinntPosition other = (VierGewinntPosition) obj;
98: return this.column == other.column && this.row == other.row;
99: }
100: return false;
101: }
102:
103: }